﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace LdCms.BLL.Sms
{
    using LdCms.EF.DbEntitiesContext;
    using LdCms.EF.DbModels;
    using LdCms.EF.DbStoredProcedure;
    using LdCms.IBLL.Sms;
    using LdCms.IDAL.Sms;
    using LdCms.Common.Json;
    using LdCms.Common.Security;
    using LdCms.Common.Extension;
    /// <summary>
    /// 
    /// </summary>
    public partial class FanWePlatformService:BaseService<Ld_Sms_FanWePlatform>,IFanWePlatformService
    {
        private readonly IFanWePlatformDAL FanWePlatformDAL;
        private readonly LdCmsDbEntitiesContext LdCmsDbEntitiesContext;
        public FanWePlatformService(LdCmsDbEntitiesContext LdCmsDbEntitiesContext, IFanWePlatformDAL FanWePlatformDAL)
        {
            this.LdCmsDbEntitiesContext = LdCmsDbEntitiesContext;
            this.FanWePlatformDAL = FanWePlatformDAL;
            this.Dal = FanWePlatformDAL;
        }
        public override void SetDal()
        {
            Dal = FanWePlatformDAL;
        }

        public bool SaveFanWePlatform(Ld_Sms_FanWePlatform entity)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string platformId = entity.PlatformID;
                bool isDefault = entity.IsDefault.ToBool();
                bool state = entity.State.ToBool();
                if (string.IsNullOrEmpty(platformId))
                {
                    var primarykey = PrimaryKeyHelper.PrimaryKeyType.SmsFanWePlatform;
                    var primaryKeyLen = PrimaryKeyHelper.PrimaryKeyLen.V1;
                    string createPlatformId = PrimaryKeyHelper.MakePrimaryKey(primarykey, primaryKeyLen);
                    entity.PlatformID = createPlatformId;
                }
                entity.IsDefault = isDefault;
                entity.State = state;
                entity.CreateDate = DateTime.Now;
                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                using (var db = dbContext.DbEntities())
                {
                    using (var trans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            if (isDefault)
                            {
                                var expression = ExtLinq.True<Ld_Sms_FanWePlatform>();
                                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                                var lists = FindList(expression).ToList();
                                lists.ForEach(m => m.IsDefault = false);
                                dbContext.Update(lists);
                            }
                            dbContext.Add(entity);
                            intnum = db.SaveChanges();
                            trans.Commit();
                        }
                        catch (Exception)
                        {
                            trans.Rollback();
                        }
                        return intnum > 0;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool UpdateFanWePlatform(Ld_Sms_FanWePlatform entity)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string platformId = entity.PlatformID;
                bool isDefault = entity.IsDefault.ToBool();
                bool state = entity.State.ToBool();
                entity.IsDefault = isDefault;
                entity.State = state;
                entity.CreateDate = DateTime.Now;
                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                using (var db = dbContext.DbEntities())
                {
                    using (var trans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            if (isDefault)
                            {
                                var expression = ExtLinq.True<Ld_Sms_FanWePlatform>();
                                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID != platformId);
                                var lists = FindList(expression).ToList();
                                lists.ForEach(m => m.IsDefault = false);
                                dbContext.Update(lists);
                            }
                            dbContext.Update(entity);
                            intnum = db.SaveChanges();
                            trans.Commit();
                        }
                        catch (Exception)
                        {
                            trans.Rollback();
                        }
                        return intnum > 0;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool UpdateFanWePlatformDefault(int systemId, string companyId, string platformId, bool isDefault)
        {
            try
            {
                var entity = Find(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId);
                if (entity == null)
                    throw new Exception("platform id invalid！");
                entity.IsDefault = isDefault;

                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                using (var db = dbContext.DbEntities())
                {
                    using (var trans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            if (isDefault)
                            {
                                var expression = ExtLinq.True<Ld_Sms_FanWePlatform>();
                                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID != platformId);
                                var lists = FindList(expression).ToList();
                                lists.ForEach(m => m.IsDefault = false);
                                dbContext.Update(lists);
                            }
                            dbContext.Update(entity);
                            intnum = db.SaveChanges();
                            trans.Commit();
                        }
                        catch (Exception)
                        {
                            trans.Rollback();
                        }
                        return intnum > 0;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool UpdateFanWePlatformState(int systemId, string companyId, string platformId, bool state)
        {
            try
            {
                var entity = Find(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId);
                if (entity == null)
                    throw new Exception("platform id invalid！");
                entity.State = state;
                return Update(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool UpdateFanWePlatformRemainPoint(int systemId, string companyId, string platformId, int remainPoint)
        {
            try
            {
                var entity = GetFanWePlatform(systemId, companyId, platformId);
                entity.RemainPoint = remainPoint;
                return Update(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool DeleteFanWePlatform(int systemId, string companyId, string platformId)
        {
            try
            {
                return Delete(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Sms_FanWePlatform GetFanWePlatform(int systemId, string companyId, string platformId)
        {
            try
            {
                return Find(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Sms_FanWePlatform GetFanWePlatformByDefault(int systemId, string companyId)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Sms_FanWePlatform>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.IsDefault == true);
                return FindList(expression, m => m.CreateDate, true).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Sms_FanWePlatform> GetFanWePlatformAll(int systemId, string companyId)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Sms_FanWePlatform>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                return FindList(expression, m => m.IsDefault, false).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public int CountFanWePlatform(int systemId, string companyId)
        {
            try
            {
                return Count(m => m.SystemID == systemId && m.CompanyID == companyId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }



    }
}
